import os
from flask import Flask
from flask_cors import CORS

import logging
from logging import StreamHandler
from applications.utils.rtsp_frame_extractor import RTSPFrameExtractor
from applications.controllers import init_bps
from applications.common.utils.custom_json_encoder import MyJSONProvider
from applications.extensions import init_plugs
from .config import BaseConfig

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('main')

# 从listeners模块导入start_data_servers函数
from applications.listeners.server import start_data_servers
from applications.listeners.udp_server import start_udp_server

Flask.json_provider_class = MyJSONProvider


def create_app():
    app = Flask(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
    # 引入配置
    app.config.from_object(BaseConfig)
    # 处理中文问题
    app.json.ensure_ascii = False
    # 处理跨域
    CORS(app)
    # 注册flask组件
    init_plugs(app)
    # 注册蓝图 (路由)
    init_bps(app)
    
    # 调用start_data_servers函数启动数据服务器
    start_data_servers(app)
    # start_udp_server(app)
    return app
